<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BWeMember
 *
 * @author jianfeng
 */
class BWeMember extends BApp {

    public function run($data = NULL) {
        parent::run($data);

        $cooker = new BAEventCooker();

        $preloadDataList = $this->getAppPreloadEventListData();
        foreach ($preloadDataList as $preloadData) {
            $cooker->preloadEvent($preloadData);
        }
        
        //test
        //$cooker1 = new BARoleCooker();
        //$cooker1->getAllSpaceUserWithRole();

        /*
          $cooker->addEventType('membership', '会员资格');
          $cooker->addEventType('memberPoint', '会员积分');
          $cooker->addEventType('memberCard', '会员卡');
         * 
         */

        //register child control
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $appAdminControl = new BUIAppAdmin('appAdmin');
        $screen->addChildControl($appAdminControl);

        //register membership control
        $membershipControl = new BUIMembership('membershipControl');
        $screen->addChildControl($membershipControl);

        //register user control
        $userControl = new BUIUser('userControl');
        $screen->addChildControl($userControl);

        //register role control
        //$roleControl = new BUIRole('roleControl');
        //$screen->addChildControl($roleControl);
        //register attribute control
        $attributeControl = new BUIAttribute('attributeControl');
        $screen->addChildControl($attributeControl);

        //register category control
        $categoryControl = new BUICategory('categoryControl');
        $screen->addChildControl($categoryControl);

        //add billyn memberPointType, this is a basic memberPointType
        /*
          $memberPointCooker = new BAMemberPointCooker();
          $memberPointCooker->addMemberPointType('BMemberPointType', '比邻积分', array(
          'currency' => 'rmb',
          'currencyUnit' => 'fen',
          'currencyRate' => '1'//one point = one fen
          ));
         * 
         */

        //add permit for addPoint/deductPoint for BWeMember::manager::memberPoint
        //$memberPointCooker->addActionPermit('BWeMember_billyMemberPointType', BAMemberPointCooker::Action_AddPoint, BAMemberPointCooker::Permit_Allow);

        $sender = Blyn::app()->getUIManager()->getSenderControl();

        if ($sender instanceof BUIOperation) {
            $screen = Blyn::app()->getUIManager()->getCurrentScreen();
            //$cooker->showEventListByOperation($screen);
            $cooker->onShowEventList($screen);
        }
    }

    //put your code here
    public function runMember($data = NULL) {
        
    }

    public function runManager($data = NULL) {
        /*
          $memberPointCooker = new BAMemberPointCooker();

          $eventCooker = new BAEventCooker();
          $eventType = $eventCooker->getEventTypeByName('memberPoint');

          //add permit for addPoint/deductPoint for BWeMember::manager::memberPoint
          $memberPointCooker->addActionPermit(
          'BMemberPointType', BAMemberPointCooker::Action_AddPoint, BAMemberPointCooker::Permit_Allow, $eventType);
          $memberPointCooker->addActionPermit(
          'BMemberPointType', BAMemberPointCooker::Action_DeductPoint, BAMemberPointCooker::Permit_Allow, $eventType);

         * 
         */
    }

    public function runAdmin($data = NULL) {
        
    }

    public function getAppPreloadEventListData() {
        return array(
            /*
              array(
              'type' => array('event.eventShare' => '功能共享'),
              'event' => array(
              'name' => 'eventShare',
              'alias' => '功能分享',
              'roles' => array(array(
              'name' => BRole::Role_Space_Admin,
              'allowModify' => 'no',
              'allowDelete' => 'no',
              'eventHome' => 'admin',
              'eventHomeTitle' => '设置共享',
              )
              )
              ),
              ),
             * 
             */
            array(
                'type' => array('appEvent.membership' => '会员资格'),
                'event' => array(
                    'name' => 'membership',
                    'alias' => '会员资格',
                    'roles' => array(
                        array(
                            'name' => BRole::Role_Space_Admin,
                            'eventHome' => 'admin',
                            'eventHomeTitle' => '设置会员资格',
                            'allowModify' => 'yes',
                        //'allowDelete' => 'yes',
                        ),
                        array(
                            'name' => BRole::Role_Space_customer,
                            'eventHome' => 'customer',
                            'eventHomeTitle' => '我的会员资格',
                        )
                    )
                )
            ),
            array(
                'type' => array('appEvent.memberPoint' => '会员积分'),
                'event' => array(
                    'name' => 'fenMemberPoint',
                    'alias' => '会员积分',
                    'roles' => array(
                        array(
                            'name' => BRole::Role_Space_Admin,
                            'eventHome' => 'admin',
                            'eventHomeTitle' => '设置积分',
                            'allowModify' => 'yes',
                        //'allowDelete' => 'yes',
                        ),
                        array(
                            'name' => BRole::Role_Space_customer,
                            'eventHome' => 'customer',
                            'eventHomeTitle' => '我的积分',
                        )
                    )
                ),
            ),
            array(
                'type' => array('appEvent.memberCard' => '会员卡'),
                'event' => array(
                    'name' => 'memberPointCard',
                    'alias' => '积分卡',
                    'roles' => array(
                        array(
                            'name' => BRole::Role_Space_Admin,
                            'eventHome' => 'admin',
                            'eventHomeTitle' => '设置会员卡',
                            'allowModify' => 'yes',
                        //'allowDelete' => 'yes',
                        ),
                        array(
                            'name' => BRole::Role_Space_customer,
                            'eventHome' => 'customer',
                            'eventHomeTitle' => '我的会员卡',
                        )
                    )
                ),
            ),
            array(
                'type' => array('appEvent.voucher' => '代金券'),
                'event' => array(
                    'name' => 'voucherEvent',
                    'alias' => '代金券',
                    'roles' => array(
                        array(
                            'name' => BRole::Role_Space_Admin,
                            'eventHome' => 'admin',
                            'eventHomeTitle' => '设置代金券',
                            'allowModify' => 'yes',
                        //'allowDelete' => 'yes',
                        ),
                        array(
                            'name' => BRole::Role_Space_customer,
                            'eventHome' => 'customer',
                            'eventHomeTitle' => '我的代金券',
                        )
                    )
                ),
            ),
        );
    }

    public function getAppEventTypeListData() {
        $eventCooker = new BAEventCooker();
        $listData = array(
            'appEvent.membership' => '会员资格',
            'appEvent.memberPoint' => '会员积分',
            'appEvent.memberCard' => '会员卡',
            'appEvent.voucher' => '代金券',
        );

        return $eventCooker->populateAvailableEventTypeListData($listData);
    }

}
